package com.gy.smart.home;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;

import com.gy.smart.R;
import com.gy.smart.base.BaseActivity;
import com.gy.smart.base.BaseBean;
import com.gy.smart.base.WebViewActivity;
import com.gy.smart.home.adapter.InteractionPhoneAdapter;
import com.gy.smart.home.adapter.InteractionSelectAdapter;
import com.gy.smart.home.bean.DynamicListBean;
import com.gy.smart.home.bean.InteractionBean;
import com.gy.smart.util.AnimUtil;
import com.gy.smart.util.RequestResultListener;
import com.gy.smart.util.Urls;
import com.gy.smart.view.MyFullGridView;
import com.gy.smart.view.MyFullListView;

import org.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：gy on 17/1/4
 * 说明：政民互动
 */
public class InteractionActivity extends BaseActivity{

    private MyFullGridView gridViewPhone;
    private MyFullListView listView;
    private InteractionPhoneAdapter phoneAdapter;
    private InteractionSelectAdapter selectAdapter;
    private List<DynamicListBean> phones,selectDatas;
    private InteractionBean bean;

    @Override
    protected void initView() {
        setContentViewRes(R.layout.activity_interaction);
        setTitleMsg("政民互动");
        gridViewPhone = (MyFullGridView) findViewById(R.id.activity_interact_gridview_phone);
        listView = (MyFullListView) findViewById(R.id.activity_interact_listview_select);
    }

    @Override
    protected void initData() {
        phones = new ArrayList<>();
        selectDatas = new ArrayList<>();
        phoneAdapter = new InteractionPhoneAdapter(this,phones);
        selectAdapter = new InteractionSelectAdapter(this,selectDatas);
    }

    @Override
    protected void setListener() {
        super.setListener();
        gridViewPhone.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra(WebViewActivity.TITLE,"昌平动态");
                intent.putExtra(WebViewActivity.URL,Urls.web_url+phones.get(position).id);
                startActivity(intent);
                AnimUtil.pushLeftInAndOut(InteractionActivity.this);
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mContext, WebViewActivity.class);
                intent.putExtra(WebViewActivity.TITLE,"昌平动态");
                intent.putExtra(WebViewActivity.URL,Urls.web_url+selectDatas.get(position).id);
                startActivity(intent);
                AnimUtil.pushLeftInAndOut(InteractionActivity.this);
            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        gridViewPhone.setAdapter(phoneAdapter);
        listView.setAdapter(selectAdapter);
        getDatas();
    }

    private void getDatas(){
        showLoadDialog(1);
        RequestParams params = new RequestParams(Urls.url_interact);
        postRequest(params, new RequestResultListener() {
            @Override
            public boolean onSuccess(BaseBean result) {
                if(result.status == 1){
                    bean = gson.fromJson(result.data,InteractionBean.class);
                    if(bean.collect != null){
                        selectDatas.addAll(bean.collect);
//                        selectAdapter.notifyDataSetChanged();
                        selectAdapter = new InteractionSelectAdapter(InteractionActivity.this,selectDatas);
                        listView.setAdapter(selectAdapter);
                    }
                    if(bean.hotline != null){
                        phones.addAll(bean.hotline);
//                        phoneAdapter.notifyDataSetChanged();
                        phoneAdapter = new InteractionPhoneAdapter(InteractionActivity.this,phones);
                        gridViewPhone.setAdapter(phoneAdapter);
                    }
                }
                return false;
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }
        });
    }
}
